package com.star.starp.utils;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author alex
 * @date 2022/1/2 17:04
 * @description:TODO
 */
//保证类是唯一实例，用单例模式
public class ThreadPoolSingle {
    public ThreadPoolExecutor poolExecutor;//原生的线程池对象
    //私有构造器 这是核心
    private ThreadPoolSingle(){
        poolExecutor=new ThreadPoolExecutor(4,10,
                3, TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(20),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());
    }
    //静态内部类  里面有一个静态的属性 属性类的对象一定是一个外部类的对象
    static class ThreadPoolSingleFactory{
        public static ThreadPoolSingle single=new ThreadPoolSingle();
    }
    //公有静态方法 (作用:获取本来对象)
    public static ThreadPoolSingle getInstance(){
        return ThreadPoolSingleFactory.single;
    }
    //新增任务
    public void addTask(Runnable runnable){
        poolExecutor.submit(runnable);
    }
}
